package structure.queue;

import java.util.Stack;

public class LeetCode_739 {

    public static void main(String[] args) {

        int[] temperatures = new int[]{73, 74, 75, 71, 69, 72, 76, 73};
        int[] temperatures1 = new int[]{30, 40, 50, 60};
        int[] ints = dailyTemperatures(temperatures);
        for (int anInt : ints) {
            System.out.println(anInt);
        }

        int[] ints1 = dailyTemperatures(temperatures1);
        for (int anInt : ints1) {
            System.out.println(anInt);
        }

    }

    /**
     * 单调栈
     *
     * @param temperatures
     * @return
     */
    public static int[] dailyTemperatures(int[] temperatures) {
        int[] res = new int[temperatures.length];
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < temperatures.length; i++) {
            while(!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]){
                Integer prev = stack.pop();
                res[prev] = i - prev;
            }
            stack.push(i);
        }
        return res;
    }


    /**
     * 两层循环搞定
     *
     * @param temperatures
     * @return
     */
    public static int[] dailyTemperatures1(int[] temperatures) {
        int[] res = new int[temperatures.length];
        for (int i = 0; i < temperatures.length; i++) {
//            Stack<Integer> stack = new Stack<>();
            for (int j = i + 1; j < temperatures.length; j++) {
                if (temperatures[j] > temperatures[i]) {
                    res[i] = j - i;
                    break;
                }
            }
        }
        return res;

    }
}
